//
//  XLHomeStatusTableViewCell.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/26.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit

class XLHomeStatusTableViewCell: UITableViewCell {
    
    // 底部视图顶部约束记录
    var bottomViewTopCons: Constraint?
    
    
    // 定义一个属性
    var statusViewModel: XLHomeViewModel?{
        didSet{
            // 给原创微博赋值
            originalView.statusViewModel = statusViewModel
            // 给转发微博赋值
            // 卸载约束
            bottomViewTopCons?.uninstall()
            // 没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
                // 隐藏转发微博view
                reweetView.isHidden = true
            }else{
                reweetView.reweetViewModel = statusViewModel
                // 有转发微博
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(reweetView.snp_bottom).constraint
                })
                layoutIfNeeded()
                // 显示retweetView
                reweetView.isHidden = false
            }
            
            // 底部视图赋值
            bottomView.bottomViewModel = statusViewModel
        }
        
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        
        // 01 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(reweetView)
        contentView.addSubview(bottomView)
        contentView.addSubview(spView)
        // 02 添加约束
        originalView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(contentView)
            // 此约束已放到原创微博中
//            make.height.equalTo(50)
        }
        
        reweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        
        bottomView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
            self.bottomViewTopCons = make.top.equalTo(reweetView.snp_bottom).constraint
            make.bottom.equalTo(contentView.snp_bottom).offset(-8)
        }
        
        spView.snp_makeConstraints { (make) in
            make.top.equalTo(bottomView.snp_bottom)
            make.left.right.bottom.equalTo(contentView)
        }
        
    }
    // 懒加载控件
    private lazy var originalView: XLStatusOriginalView = XLStatusOriginalView()
    private lazy var reweetView: XLStatusRetweetView = XLStatusRetweetView()
    private lazy var bottomView: XLStatusBottomView = XLStatusBottomView()
    private lazy var spView: XLSpView = XLSpView()

}
